package com.ccsoft.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

// 演示从本地文件读取数据
public class Ch02ReadChannel {
    public static void main(String[] args) throws IOException {
        // 准备 channel
        File file = new File("d:\\filechannel.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileChannel channel = fileInputStream.getChannel();

        // 准备缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        channel.read(byteBuffer);

        // 打印读取到的内容
        System.out.println(new String(byteBuffer.array()));
        channel.close();
    }
}
